package com.roshan.web.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.roshan.domain.Contact;
import com.roshan.domain.MfaQuestion;
import com.roshan.domain.SecurityUser;
import com.roshan.domain.User;
import com.roshan.service.ContactService;
import com.roshan.service.MfaQuestionService;
import com.roshan.service.RoleService;
import com.roshan.service.UserService;

@SuppressWarnings("nls")
@Controller
public class ContactController {

    @Autowired
    private ContactService contactService;

    @Autowired
    private UserService userService;

    @Autowired
    private MfaQuestionService mfaQuestionService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/")
    public String home(Model model) {

        model.addAttribute("contact", new Contact());
        model.addAttribute("contactList", this.contactService.getAll());

        MfaQuestion mfaQuestion1 = new MfaQuestion();
        mfaQuestion1.setQuestion("What is your first girl friend's name?");

        this.mfaQuestionService.save(mfaQuestion1);

        MfaQuestion mfaQuestion2 = new MfaQuestion();
        mfaQuestion2.setQuestion("What is your grade 1 teacher's name?");

        this.mfaQuestionService.save(mfaQuestion2);

        MfaQuestion mfaQuestion3 = new MfaQuestion();
        mfaQuestion3.setQuestion("What is your childhood friend's name?");

        this.mfaQuestionService.save(mfaQuestion3);

        return "contact";
    }

    @RequestMapping("/index")
    public String listContacts(Map<String, Object> map) {

        map.put("contact", new Contact());
        map.put("contactList", this.contactService.getAll());

        return "contact";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact") Contact contact, BindingResult result) {
        if (result.hasErrors()) {
            // nothing
        }
        this.contactService.save(contact);

        User user = new User();

        user.setEmail(contact.getEmail());

        SecurityUser securityUser = new SecurityUser();
        securityUser.setUsername(contact.getFirstname());
        securityUser.setPassword(contact.getLastname());

        user.setSecurityUser(securityUser);

        this.userService.save(user);

        return "redirect:/index";
    }

    @RequestMapping("/delete/{contactId}")
    public String deleteContact(@PathVariable("contactId") Long contactId) {

        this.contactService.remove(contactId);

        return "redirect:/index";
    }
}
